<template>
  <div>
    <!-- 上部卡片区域 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item><a href="/">内容管理</a></el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 表单区域 -->
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="mycom">
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 引入频道组件 -->
        <my-channel v-model="form.channelId"></my-channel>
        <el-form-item label="日期">
          <el-col :span="11">
            <el-date-picker
              v-model="form.dateRange"
              value-format = 'yyyy-MM-dd'
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button
          :disabled='isLoading'
          :loading="isLoading"
          type="primary"
          @click="query">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 下部卡片区域 -->
    <el-card class="box-card" style="margin-top: 20px">
      <div slot="header" class="clearfix">
        <span>根据查询条件，找到{{total_count}}条记录</span>
      </div>
      <el-table
        v-loading="isLoading"
        element-loading-text="拼命加载中"
        :data="articles"
        style="width: 100%">
         <el-table-column
          label="封面"
          width="280">
          <template slot-scope="scope">
            <el-image :src="scope.row.cover.images[0]">
              <div slot="placeholder" class="image-slot">
                加载中<span class="dot">...</span>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          width="400">
        </el-table-column>
        <el-table-column label="状态" width="180">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status|fStatusType">{{scope.row.status|fStatusText}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="pubdate"
          label="发布时间">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              @click="handleEdit(scope.row.id.toString())"></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row.id.toString())"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination style="margin-top:15px"
        @current-change = 'pageChange'
        :hide-on-single-page="true"
        background
        layout="prev, pager, next"
        :total="total_count">
      </el-pagination>
    </el-card>
    <!-- 表格区域 -->
  </div>
</template>
<script>
export default {
  data () {
    return {
      form: {
        dateRange: [],
        status: null,
        channelId: null
      },
      articles: [],
      total_count: 0,
      page: 1,
      isLoading: false
    }
  },
  methods: {
    // 查询文章
    query () {
      this.isLoading = true
      this.getArticle()
    },
    // 构造查询的参数，通过函数的返回值获取结果
    getParams () {
      const { dateRange, status, channelId } = this.form
      // 获取page\status
      const params = {
        page: this.page,
        status
      }
      // 获取channelId
      if (channelId === '') {
        params.channel_id = null
      } else {
        params.channel_id = channelId
      }
      // 获取begin_pubdate、end_pubdate
      if (dateRange) {
        params.begin_pubdate = dateRange[0]
        params.end_pubdate = dateRange[1]
      }
      // 返回结果
      return params
    },
    // 获取文章列表
    async getArticle () {
      // 设计查询条件：status、channel_id、begin_pubdate、end_pubdate、page
      const params = this.getParams()
      console.log('本次查询的条件对象是', params)
      try {
        const res = await this.$axios({
          url: '/mp/v1_0/articles',
          method: 'get',
          // params: {
          //   page: this.page,
          // }
          params
        })
        console.log(res)
        this.articles = res.data.data.results
        this.total_count = res.data.data.total_count
      } catch (err) {
        this.$message.error('获取个人信息失败')
        console.log(err)
      }
      this.isLoading = false
    },
    // 当前页码数据传输
    pageChange (currentPage) {
      console.log('当前页码是', currentPage)
      this.page = currentPage
      this.getArticle()
    },
    // 删除文章
    async doDelete (id) {
      try {
        const res = await this.$axios({
          url: '/mp/v1_0/articles/' + id,
          method: 'DELETE'
        })
        console.log(res)
      } catch (err) {
        console.log(err)
      }
      this.getArticle()
    },
    handleDelete (id) {
      this.$confirm('你确定要删除吗').then(() => {
        this.doDelete(id)
      }).catch(() => {})
    },
    // 编辑文章
    handleEdit (id) {
      console.log(id)
      this.$router.push('/edit/' + id)
      // this.$router.push('/edit/' + id)
    }
  },
  // 生命周期函数，进入页面即执行获取数据
  created () {
    this.getArticle()
  },
  filters: {
    fStatusType (status) {
      const types = {
        0: 'success',
        1: 'info',
        2: 'normal',
        3: 'warning',
        4: 'danger'
      }
      return types[status]
    },
    fStatusText (status) {
      const types = {
        0: '草稿',
        1: '待审核',
        2: '审核通过',
        3: '审核失败',
        4: '已删除'
      }
      return types[status]
    }
  }
}
</script>

<style lang='less' scoped>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }
  .el-image__inner{
    width: 150px;
    height: 100px;
  }
</style>
